﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.PRM.AppCode.Lib;

namespace mojoPortal.Web.RS.PRM.Controls
{
    public partial class PropertyWithOutOwnerControl : RSBaseUserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int ownerid = 0;
                RSUserIdentity userIdentity = GetRSUserIdentity();
                if (Request.QueryString["ownerid"] != null)
                {
                    ownerid = int.Parse(Request.QueryString["ownerid"].ToString());
                }
                hfOwnerID.Value = ownerid.ToString();
                if (ownerid > 0)
                {
                    ucOwnerHeaderControl.LoadOwnerHeader(userIdentity.RSCompanyId, ownerid);
                    LoadPropertyWithOutOwnerList(userIdentity.RSCompanyId);
                }
            }
        }
        protected void btnAddPropertyToOwner_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int ownerid = int.Parse(hfOwnerID.Value);
            if (ownerid > 0)
            {
                int propertyid = Convert.ToInt32(e.CommandArgument.ToString());
                //Add this property to this owner
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@OwnerID", SqlDbType.Int, 0, ParameterDirection.Input, ownerid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@PropertyID", SqlDbType.Int, 0, ParameterDirection.Input, propertyid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, userIdentity.RSCompanyId);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "update rs_tblproperty set ownerid=@ownerid where propertyid=@propertyid and companyid=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                ucModalActionMessageControl.ShowModal("Property Successfully Added to Owner!", "RSViewOwnerList.aspx", true);
           }
            else
                ucModalActionMessageControl.ShowModal("Owner Has Not Been Selected!", "", false);
        }
        protected void gvProperty_Edit(object sender, CommandEventArgs e)
        {
            int propid = Convert.ToInt32(e.CommandArgument.ToString());
            if (propid > 0)
                Response.Redirect("RSPropertyDetail.aspx?propid=" + propid);
        }
        protected void lnkMapIt_Click(object sender, CommandEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            int propid = Convert.ToInt32(e.CommandArgument.ToString());
            if (companyid > 0 && propid > 0)
                ucModalViewMapControl.ShowMapDataByPropertyID(companyid, propid);
        }


        private bool LoadPropertyWithOutOwnerList(int companyid)
        {
            bool bHasValue = true;

            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int managerid = userIdentity.RSEmployeeId;
            String sql = "exec rs_uspGetPropertyWithOutOwnerByManagerID @CompanyID, @ManagerID";

            if (Request.QueryString["manid"] != null)
            {
                if(RSPRMCommonFunctions.IsNumeric(Request.QueryString["manid"].ToString()))
                    managerid = int.Parse(Request.QueryString["manid"].ToString());
            }
            if(userIdentity.RSGroupName == "DIRECTOR")
                sql = "exec rs_uspGetPropertyWithOutOwnerByCompanyID @CompanyId";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sql, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@ManagerID";
            param.Value = managerid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            //Session["DATATABLE"] = ds.Tables[0];
            gvPropertyList.DataSource = ds;
            gvPropertyList.DataBind();
            myConnection.Close();

            if (gvPropertyList.Rows.Count == 0)
            {
                bHasValue = false;
                return bHasValue;
            }
            return bHasValue;
        }
        
        //private string ConvertSortDirectionToSql(SortDirection sortDirection)
        //{
        //    string newSortDirection = String.Empty;

        //    switch (sortDirection)
        //    {
        //        case SortDirection.Ascending:
        //            newSortDirection = "ASC";
        //            break;

        //        case SortDirection.Descending:
        //            newSortDirection = "DESC";
        //            break;
        //    }
        //    return newSortDirection;
        //}

        protected void gvPropertyList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPropertyList.PageIndex = e.NewPageIndex;
            gvPropertyList.DataBind();
        }

        //protected void gvPropertyList_Sorting(object sender, GridViewSortEventArgs e)
        //{
        //    DataTable dataTable = Session["DATATABLE"] as DataTable;
        //    if (dataTable != null)
        //    {
        //        DataView dataView = new DataView(dataTable);
        //        dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

        //        gvPropertyList.DataSource = dataView;
        //        gvPropertyList.DataBind();
        //    }
        //}

    }
}